package org.testfire.agent.tags;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import org.testfire.agent.action.*;

/**
 * Created by IntelliJ IDEA.
 * User: granum
 * Date: Sep 30, 2007
 * Time: 12:54:13 PM
 *
 */
public class SubmitTag extends SimpleTagSupport
{

    public static final String ACTION_SAVE = "ACTION_SAVE";

    public static final String ACTION_DELETE = "ACTION_DELETE";

    public static final String ACTION_RUN = "ACTION_RUN";
    
    public static final String ACTION_TO_WATIJ = "ACTION_TO_WATIJ";

    String action;

    public void doTag() throws JspException, IOException
    {
        ServletRequest request = ((PageContext)super.getJspContext()).getRequest();

        Action act;
        if ( action.equals( ACTION_SAVE ) )
        {
            act = new SaveAction();
        }
        else if ( action.equals( ACTION_DELETE ) )
        {
            act = new DeleteAction();
        }
        else if ( action.equals( ACTION_RUN ) )
        {
            act = new RunAction();
        }
        else if (action.equals( ACTION_TO_WATIJ ) )
        {
            act = new ToWatijTranslateAction();
        }
        else
        {
            System.out.println( "Invalid Action." );
            return;
        }

        Map<String, String> params = new HashMap<String, String>();
        Map<String, String[]> paramValues = new HashMap<String, String[]>();
        if ( act.getRequiresAllParameters() )
        {
            Enumeration en = request.getParameterNames();
            Map m = request.getParameterMap();
            while(en.hasMoreElements()){
                String key = (String)en.nextElement();
                Object o = m.get( key );
                if(o instanceof String){
                    params.put( key, (String)o );
                }
                else if(o instanceof String[]){
                    paramValues.put( key, (String[])o );
                }
            }

        }
        else
        {
            List<String> expectedParams = act.getExpectedParams();
            List<String> expectedParamValues = act.getExpectedParamValues();


            for ( String p : expectedParams )
            {
                params.put( p, request.getParameter( p ) );
            }

            for ( String p : expectedParamValues )
            {
                paramValues.put( p, request.getParameterValues( p ) );
            }
        }

        try
        {
            String result = act.perform( params, paramValues );
            this.getJspContext().getOut().print( result );
        }
        catch ( Exception e )
        {
            e.printStackTrace();
        }
    }

    public String getAction()
    {
        return action;
    }

    public void setAction( String action )
    {
        this.action = action;
    }
}